// pages/wode/merchant.js
const app=getApp()
const util=require('../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bankList:[
      "中国银行",
      "工商银行",
      "建设银行",
      "农业银行",
      "招商银行",
      "平安银行",
      "民生银行",
      "中信银行",
      "邮政储蓄银行",
    ],
    bankNameIndex:-1,
    isLong:false,
    isClick:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  pickerChange:function(e){
    console.log(e)
    var name=e.currentTarget.dataset.name
    var key=name+'Index'
    var val=e.detail.value
    this.setData({
      [key]:val
    })
  },
  pickerDateChange:function(e){
    console.log(e)
    var name=e.currentTarget.dataset.name
    
    var val=e.detail.value
    this.setData({
      [name]:val
    })
  },
  checkboxChange:function(e){
    console.log(e)
    var val=e.detail.value
    if(val.length>0){
      this.setData({
        contactIdPeriodEnd:'2199-12-31',
        isLong:true
      })
    }else{
      this.setData({
        contactIdPeriodEnd:'',
        isLong:false
      })
    }
  },
  submit:function(e){
    console.log(e)
    var that=this
    var data=e.detail.value
    var bankList=this.data.bankList
    var bankNameIndex=this.data.bankNameIndex
    var mediaIdCopy=this.data.mediaIdCopy
    var mediaIdNational=this.data.mediaIdNational
    var bankAccountPic=this.data.bankAccountPic
    var mobileReg=/^1[3-9]\d{9}$/
    var nameReg=/^[\u4e00-\u9fa5\s]{2,10}$/
    var emailReg=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,20}$/;
    var tips=''
    if(!data.bankCardName){
      tips='请输入您的真实姓名'
    }else if(data.bankCardName && !nameReg.test(data.bankCardName)){
      tips='请输入正确的姓名'
    }else if(!data.mobile){
      tips='请输入您的手机号码'
    }else if(data.mobile && !mobileReg.test(data.mobile)){
      tips='请输入正确的手机号码'
    }else if(!data.cardNo){
      tips='请输入您的身份证号'
    }else if(data.cardNo && !util.validateidcard(data.cardNo)){
      tips='请输入正确的身份证号'
    }else if(!data.idDocAddress){
      tips='请输入您的居住地址'
    }else if(!data.email){
      tips='请输入您的电子邮箱'
    }else if(data.email && !emailReg.test(data.email)){
      tips='请输入正确的电子邮箱'
    }else if(!data.contactIdPeriodBegin){
      tips='请选择身份证有效期开始时间'
    }else if(!data.contactIdPeriodEnd){
      tips='请选择身份证有效期结束时间'
    }else if(!mediaIdCopy){
      tips='请上传身份证正面照片'
    }else if(!mediaIdNational){
      tips='请上传身份证反面照片'
    }else if(!data.bankCardName){
      tips='请输入开户名'
    }else if(!data.bankCard){
      tips='请输入银行卡号'
    }else if(bankNameIndex==-1){
      tips='请选择开户银行'
    }else if(!bankAccountPic){
      tips='请输入银行卡照片'
    }
  
    if(tips){
      wx.showToast({
        title: tips,
        icon:'none',
        duration:1500,
        mask:true
      })
      return false
    }


  data.mediaIdCopy=mediaIdCopy
  data.mediaIdNational=mediaIdNational
  data.bankAccountPic=bankAccountPic

    data.bankName=bankList[bankNameIndex]
    //data.realName=data.bankCardName
    data.storeId=wx.getStorageSync('userInfo').storeId
    console.log(data)
    delete data.isLong

    var isClick=this.data.isClick
   
    if(isClick){
      return false
    }else{
      that.setData({
        isClick:true
      })
    }
    setTimeout(function(){
      that.setData({
        isClick:false
      })
    },1500)
    wx.showLoading({
      mask:true
    })
    app.agriknow.storeApply(data).then(res=>{
      console.log(res)
      wx.hideLoading()
      if(res.code=='000000'){
  
        wx.showToast({
          title: '进件成功',
          icon:'success',
          duration:1500,
          mask:true
        })
        var userInfo=wx.getStorageSync('userInfo')
        userInfo.isApply=1
        wx.setStorageSync('userInfo', userInfo)
       setTimeout(function(){
        wx.switchTab({
          url: '/pages/wode/index',
        })
       },1500)
      }else{
        wx.showToast({
          title: res.data,
          icon: 'none',
          duration: 2000,
          mask: true
        })
        
      }
    })
  },
  refresh: function() {
    // 触发页面的事件处理函数
    this.triggerEvent('refresh')
  },

  urlTobase64(url){
 
    return new Promise(function (resolve, reject) {
      wx.getFileSystemManager().readFile({
        filePath: url,
        encoding: "base64",
        success: function(res) {
          let base64 = 'data:image/png;base64,' + res.data; 
         
          resolve(base64)
        }
    })
  })
 },
  uploadImg(e) {
    let key = e.currentTarget.dataset.name
    let that = this
    wx.chooseMedia({
      count:1,
      success (res) {
        console.log(res.tempFiles[0].tempFilePath)
        const tempFilePaths = res.tempFiles[0].tempFilePath
        wx.uploadFile({
          url: app.agriknow._baseUrl+'ht/store/uploadSftImage', //仅为示例，非真实的接口地址
          filePath: tempFilePaths,
          name: 'image',
          timeout:10000,
          formData: {
            'user': 'test'
          },
          success (res){
            var datas=JSON.parse(res.data)
            if(datas.code=='000000'){

            
            that.setData({
              [key]:datas.data
            })
            var url=key+'Url'
            that.urlTobase64(tempFilePaths).then(re=>{
              that.setData({
                [url]:re
              })
            })
          }else{
            wx.showToast({
              title: datas.message,
              icon:'none',
              duration:1500,
              mask:true
            })
          }
           
            
          }
        })
      }
    })
  },


})